import { describe, it, expect, vi, beforeEach } from 'vitest';
import { isPiniaActive, safeGetUserStore, safeGetAppStore } from '../storeUtils';

// Mock pinia
vi.mock('pinia', () => ({
  getActivePinia: vi.fn()
}));

describe('storeUtils', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('isPiniaActive', () => {
    it('should return true when pinia is active', async () => {
      const { getActivePinia } = await import('pinia');
      vi.mocked(getActivePinia).mockReturnValue({} as any);
      
      expect(isPiniaActive()).toBe(true);
    });

    it('should return false when pinia is not active', async () => {
      const { getActivePinia } = await import('pinia');
      vi.mocked(getActivePinia).mockReturnValue(undefined as any);
      
      expect(isPiniaActive()).toBe(false);
    });

    it('should return false when getActivePinia throws', async () => {
      const { getActivePinia } = await import('pinia');
      vi.mocked(getActivePinia).mockImplementation(() => {
        throw new Error('Pinia not active');
      });
      
      expect(isPiniaActive()).toBe(false);
    });
  });

  describe('safeGetUserStore', () => {
    it('should return null when pinia is not active', async () => {
      const { getActivePinia } = await import('pinia');
      vi.mocked(getActivePinia).mockReturnValue(undefined as any);
      
      const result = await safeGetUserStore();
      expect(result).toBe(null);
    });
  });

  describe('safeGetAppStore', () => {
    it('should return null when pinia is not active', async () => {
      const { getActivePinia } = await import('pinia');
      vi.mocked(getActivePinia).mockReturnValue(undefined as any);
      
      const result = await safeGetAppStore();
      expect(result).toBe(null);
    });
  });
});